﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mogre;

namespace ProjetSMA.View.Mogre
{
    public class RayLine
    {
        private Entity3D m_first;
        private Entity3D m_second;

       // private Entity3D m_second;

        private SceneManager m_scmgr;
        private string m_name;
        private Root m_root;
        private SceneNode m_node;

        private List<SceneNode> m_list_node = new List<SceneNode>();


        private Dictionary<string, SceneNode> m_list_key_node = new Dictionary<string, SceneNode>();
        //private List<string> m_list_name = new List<string>();

        public RayLine(Root par_root, SceneManager par_sc, string par_name, Entity3D par_first, Entity3D par_second)
        {
            m_first  = par_first;
            m_second = par_second;
            m_scmgr  = par_sc;
            m_name = par_name;
            m_root = par_root;
        }

        public void Initialisation()
        {
            m_root.FrameStarted += new FrameListener.FrameStartedHandler(Move_FrameStarted);

            
            // create resource group
            String resourceGroupName = "debugger";
            if (ResourceGroupManager.Singleton.ResourceGroupExists(resourceGroupName) == false)
                ResourceGroupManager.Singleton.CreateResourceGroup(resourceGroupName);

            // create material (colour)
            MaterialPtr moMaterial = MaterialManager.Singleton.Create("line_material", resourceGroupName);
            moMaterial.ReceiveShadows = false;
            //moMaterial.GetTechnique(0).SetLightingEnabled(true);
            moMaterial.GetTechnique(0).GetPass(0).SetSceneBlending(SceneBlendType.SBT_TRANSPARENT_ALPHA);
            moMaterial.GetTechnique(0).GetPass(0).DepthWriteEnabled = false;
            moMaterial.GetTechnique(0).GetPass(0).SetDiffuse(0.3f, 0.3f, 0.5f, 0.05f);
           // moMaterial.GetTechnique(0).GetPass(0).SetAmbient(0.5f, 0.5f, 0.5f);
           // moMaterial.GetTechnique(0).GetPass(0).SetSelfIllumination(0.3f, 0.3f, 0.5f);
            
            moMaterial.Dispose();
            

            float center_x = m_second.Node.Position.x;
            float center_y = m_second.Node.Position.y;

            float new_coord_x = 0;
            float new_coord_y = 0;

            float loc_rayon = 10.0f;
            string loc_name = "";

            for (float angle = 0; angle < 360.0f; angle +=  6)
            {

                loc_name =  m_name + angle;
                new_coord_x = (loc_rayon * (float)System.Math.Cos(angle)) + center_x;
                new_coord_y = (loc_rayon * (float)System.Math.Sin(angle)) + center_y;

                // create line object
                ManualObject manOb = m_scmgr.CreateManualObject("line" + loc_name);
                manOb.Begin("Examples/TransparentTest", RenderOperation.OperationTypes.OT_LINE_LIST);
                //manOb.Begin("line_material", RenderOperation.OperationTypes.OT_LINE_LIST);
                manOb.Position(m_first.Node.Position.x, m_first.Node.Position.y, m_first.Node.Position.z);
                manOb.Position(new_coord_x, new_coord_y, m_second.Node.Position.z);

                manOb.End();

                SceneNode loc_sc = m_scmgr.RootSceneNode.CreateChildSceneNode("line_node" + loc_name);
                loc_sc.Position = new Vector3(0, 23, 0);
                loc_sc.AttachObject(manOb);

                m_list_key_node.Add(loc_name, loc_sc);

               // m_list_node.Add(loc_sc);
            }

           // Console.WriteLine("x:"+m_first.Node.Position.x+" y:"+ m_first.Node.Position.y+ " z:"+ m_first.Node.Position.z);

        }

        public bool Move_FrameStarted(FrameEvent evt)
        {
            foreach (KeyValuePair<string, SceneNode> pair in m_list_key_node)
            {
                pair.Value.Translate(m_first.Direction * m_first.Speed);
            }

    
            return true;
        }

        public void Destroy()
        {
            m_root.FrameStarted -= new FrameListener.FrameStartedHandler(Move_FrameStarted);

            foreach (KeyValuePair<string, SceneNode> pair in m_list_key_node)
            {
                // destroy the line
                m_scmgr.DestroyManualObject("line" + pair.Key);

                // destroy the SceneNode (or keep it to add other manual objects)
                m_scmgr.DestroySceneNode("line_node" + pair.Key);     
            }
           
        }

    }
}
